PascalCII
Volume Number: 10
Issue Number: 1
Column Tag: Pascal/C workshop
The Pascal Programmer’s Guide
To Understanding ‘C’
Teach yourself to read another language - Part II
By Ken Gladstone, MacTech Magazine Technical Editor
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
This article is the second half of my Pascal Programmer’s Guide to Understanding
“C”. If you haven’t already, I suggest you read the first half, which appeared in our
December 1993 issue - otherwise you will probably be thoroughly confused by this
half! That first half covered the following “C” concepts: comments, identifiers,
operators, constants, program structure, and variable declarations and scope. So,
let’s continue
PARAMETER PASSING
One key difference between C and Pascal is that C always passes parameters by
value, never by reference. Therefore, you may be wondering how a C function can ever
modify a passed in parameter. It can’t - but you can accomplish the same thing by
passing a pointer to the value you wish to modify, and having the function modify the
pointed to value. Here is an example:
/* 1 */
/************************ C Version *************************/
void doubleIt( int * pointerToIntParam )
{
(*pointerToIntParam) *= 2;
}
int main()
{
int myInt = 3;
doubleIt( & myInt );
return myInt;
}
/******************** End of C Version **********************/
(********************* Pascal Version ***********************)
program myProgram;
var
myInt: INTEGER;
procedure doubleIt( var IntParam: INTEGER );
BEGIN
IntParam := IntParam * 2
END;
BEGIN
myInt := 3;
doubleIt( myInt )
END.
(****************** End of Pascal Version *******************)
OLD VERSUS NEW FUNCTION DECLARATIONS
So far, I’ve been showing functions as follows:
int MyFunc( int a, char b, float c )
{
/* Code goes here */
}
This way of writing functions is an ANSI extension that allows C to perform
parameter type checking when calling a function. Things weren’t always so nice. In
the original K&R C, functions were written as follows:
int MyFunc( a, b, c )
int a;
char b;
float c;
{
/* Code goes here */
}
In original C compilers, when calling a function, there was no checking of
parameter types, or often even of the number of parameters! In old C, you could write
a call to a function before it had ever been defined, declared or mentioned in any way!
Now, C compilers have much stronger type-checking. For example, Think C has a
compiler option to require you to write a function prototype for every function.
FLOW CONTROL
So far, all of the examples that I’ve shown execute code sequentially - in fact,
I’ve only shown declarations, assignment statements, function calls, and function
return statements. Like Pascal, C has various loops and other constructs to control the
flow of code. We’ll start with the while loop. The while loop in C is nearly identical to
the one in Pascal, except that it needs parens around the test expression and it doesn’t
have a DO keyword. Examples:
/* 2 */
while ( i < j ) i *= 2; // First C example
while i < j DO i := i * 2; {Pascal Equiv.}
while ( i < j ) // C example w/compound statement
{
sysBeep( 1 );
i *= 2;
}
while i < j DO {Pascal Equiv.}
BEGIN
sysBeep( 1 );
i := i * 2
END
Next we have the C do statement. This is a loop with the test at the end of each
iteration, like the Pascal REPEAT statement, but the sense of the while test at the end
is the opposite of the Pascal UNTIL test. Unlike the Pascal version, the C version needs
braces if the loop contains a compound statement. And again, the while condition needs
parens. Example:
/* 3 */
do // C version
{
sysBeep( x );
++ x;
}
while ( x != 10 );
REPEAT {Pascal equiv.}
sysBeep( x );
x := x + 1
UNTIL x = 10;
Next we have the for loop. The for loop in C is far more general than the one in
Pascal. It looks like this:
for ( expr1; expr2; expr3 )